<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">
	
	<script src="js/jquery.min.js"></script>

</head>
<body>
    <div class="nav">
        <div class="flag">
            <img src="image/pen.svg" alt="logo">
            <div class="title">宝克书写笔销售系统</div>
        </div>

        <div class="page">
			<a href="login.html">登录</a>
        </div>
    </div>
    
    <div class="login_container">
        <div class="login_dialog">
			<table>
				<tr>
					<th colspan="2">注册</th>
				</tr>
				<tr>
					<td class="t1">用户名</td>
					<td><input type="text" id="username"></td>
				</tr>
				<tr>
					<td class="t1">密码</td>
					<td><input type="password" id="password"></td>
				</tr>
				<tr>
					<td class="t1">确认密码</td>
					<td><input type="password" id="password2"></td>
				</tr>
				<tr>
					<td colspan="2"><input type="submit" value="提交" id="submit" onclick="reg()"></td>
				</tr>
			</table>
        </div>
    </div>
	<script>
	        function reg(){
	            // 1.校验参数
	            var username = jQuery("#username");
	            var password = jQuery("#password");
	            var password2 = jQuery("#password2");
	            if (username.val().trim() == "") {
	                alert("请输入用户名!");
	                usename.focus();
	                return false;
	            }
	            if (password.val().trim() == "") {
	                alert("请输入密码!");
	                password.focus();
	                return false;
	            }
	            if (password2.val().trim() == "") {
	                alert("请确认密码!");
	                password2.focus();
	                return false;
	            }
	            if (password2.val().trim() != password.val().trim()) {
	                alert("两次密码不一致!");
	                return false;
	            }
	            // 2.将参数提交给后端
	            jQuery.ajax({
	                url:"/user/reg",
	                type:"POST",
	                data:{
	                    "username":username.val().trim(),
	                    "password":password.val().trim(),
						"passwordRepeat":password.val().trim()
	                },
	                success:function(res){
	                    // 3.将结果返回给用户
	                    if (res.code == 200) {
	                        // 注册成功
	                        alert("注册成功!");
	                        // 跳转到登录页
	                        location.href = "login.html";
	                    } else {
	                        // 注册失败
	                        alert("抱歉注册失败!"+res.meg);
	                    }
	                }
	            });
	        }
	    </script>
</body>
</html>